<template>
  <div class="result-template pad">
    <div class="head">
      <span>添加测评结果模板</span>
    </div>
    <el-form label-position="top" :model="resultTemplate" :rules="rules" ref="result" class="m-t-b">
      <el-form-item label="年龄" prop="age">
        <el-input type="number" v-model="resultTemplate.age" />
      </el-form-item>
      <el-form-item label="性别" prop="sex">
        <el-select v-model="resultTemplate.sex">
          <el-option :value="0" label="女"></el-option>
          <el-option :value="1" label="男"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="结果">
        <el-select v-model="resultTemplate.bmiResultId">
          <el-option v-for="item in results" :key="item.id" :value="item.id" :label="item.name"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="缺乏症">
        <el-select v-model="resultTemplate.ageusiaId">
          <el-option v-for="(item, i) in lacks" :key="i" :value="item.ageusiaId" :label="item.ageusia"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="缺乏症建议" prop="context">
        <el-input type="textarea" rows="5" v-model="resultTemplate.context"></el-input>
      </el-form-item>
    </el-form>
    <el-button type="primary" @click="addResultTemplate('result')">添加</el-button>
  </div>
</template>
<script>
import { getAllLackList, addResultTemplate } from '../api';

export default {
  name: 'resultTemplate',
  data () {
    return {
      resultTemplate: {
        age: '',		//int	年龄
        sex: '',		//int	性别(1男 0女)
        bmiResultId: '',  //Long   结果症状id
        ageusiaId: '',	//Long   缺乏症id
        context: ''		//文本描述
      },
      lacks: [],
      rules: {
        age: [
          { required: true, message: '该项不能为空', trigger: 'blur' },
          { min: 0, max: 18, message: '年龄必须在0 - 18岁之间', trigger: 'blur' }
        ],
        sex: [
          { required: true, message: '该项不能为空', trigger: 'blur' }
        ],
        bmiResultId: [
          { required: true, message: '该项不能为空', trigger: 'blur' }
        ],
        ageusiaId: [
          { required: true, message: '该项不能为空', trigger: 'blur' }
        ],
        context: [
          { required: true, message: '该项不能为空', trigger: 'blur' }
        ]
      },
      results: [
        { name: '消瘦', id: 26 },
        { name: '肥胖', id: 24 },
        { name: '超重', id: 25 }
      ]
    }
  },
  methods: {
    addResultTemplate (name) {
      this.$refs[name].validate(valid => {
        if (valid) {
          addResultTemplate({
            age: Number(this.resultTemplate.age),		//int	年龄
            sex: this.resultTemplate.sex,		//int	性别(1男 0女)
            bmiResultId: this.resultTemplate.bmiResultId,  //Long   结果症状id
            ageusiaId: this.resultTemplate.ageusiaId,	//Long   缺乏症id
            context: this.resultTemplate.context		//文本描述
          })
          .then(res => {
            if (res.data.code === 1) {
              this.$message({
                type: 'success',
                message: '添加成功!'
              })
            } else {
              this.$message.error(res.data.msg || '网络异常!')
            }
          })
          .catch(e => {
            this.$message.error('网络异常!')
          })
        } else {
          return false
        }
      })
    },
    fetchAllLack () { // 获取所有的缺乏症
      getAllLackList()
      .then(res => {
        if (res.data.code === 1) {
          let list = res.data.data || [];
          this.lacks = list
        } else {
          this.$message.error(res.data.msg || '网络异常!')
        }
      })
      .catch(e => {
        this.$message.error('网络异常!')
      })
    }
  },
  created () {
    this.fetchAllLack()
  }
}
</script>
<style lang="scss">
  .result-template {

  }
</style>
